import sys
from PyQt5.QtWidgets import (
    QApplication, QWidget, QLabel, QLineEdit, QPushButton, QVBoxLayout, QHBoxLayout, QFormLayout
)
from PyQt5.QtGui import QMovie, QFont
from PyQt5.QtCore import Qt, QTimer, QDateTime
from util.config import Config

class LoginWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle("登录页面")
        self.setGeometry(0, 0, 680, 400)
        #self.setGeometry(100, 100, 800, 600)  # 设置初始窗口大小

        self.config = Config()
        # 主布局
        main_layout = QVBoxLayout(self)
        main_layout.setContentsMargins(0, 0, 0, 0)

        # 1. 顶部区域 (Header)
        header_widget = QWidget(self)
        header_widget.setFixedHeight(30)
        header_widget.setStyleSheet("image: url(views/assets/header_bg.png);")
        header_layout = QHBoxLayout(header_widget)
        #header_layout.setContentsMargins(10, 10, 10, 10)

        # 悬浮标题
        title_label = QLabel("系统登录", self)
        title_label.setStyleSheet("color: white; font-size: 24px; font-weight: bold;")
        header_layout.addWidget(title_label, alignment=Qt.AlignCenter)

        main_layout.addWidget(header_widget)
        # 时间显示区域
        '''
        self.label_time = QLabel(self)
        self.label_time.setFont(QFont("Arial", 14))
        self.label_time.setStyleSheet("color: white;")
        self.update_time()
        header_layout.addWidget(header_widget,alignment=Qt.AlignLeft)
        '''
        # 2. 动态背景和左侧时间区域
        background_widget = QWidget(self)
        background_layout = QHBoxLayout(background_widget)
        background_layout.setContentsMargins(0, 0, 0, 0)
        background_widget.setStyleSheet("""
            QWidget {
                image: url(views/assets/bg5.png);
                background-color:rgba(0,0,0,0);    
            }
        """)

    
        main_layout.addWidget(background_widget, stretch=1)
        

        # 3. 中间区域：登录表单
        form_widget = QWidget(self)
        
        #form_widget.setStyleSheet("background: rgba(255, 255, 255, 0.8); border-radius: 10px;")
        form_widget.setMaximumWidth(400)

        form_layout = QFormLayout(form_widget)
        form_layout.setContentsMargins(20, 20, 20, 20)

        # 用户名输入
        user_label = QLabel("用户名:")
        self.user_edit = QLineEdit()
        self.user_edit.setPlaceholderText("请输入用户名")
        form_layout.addRow(user_label, self.user_edit)

        # 密码输入
        pwd_label = QLabel("密 码:")
        self.pwd_edit = QLineEdit()
        self.pwd_edit.setEchoMode(QLineEdit.Password)
        self.pwd_edit.setPlaceholderText("请输入密码")
        form_layout.addRow(pwd_label, self.pwd_edit)

        # 登录按钮
        self.login_button = QPushButton("登录")
        self.login_button.setStyleSheet("background-color: #4CAF50; color: white; font-size: 16px;")
        form_layout.addRow(self.login_button)

        background_layout.addWidget(form_widget, alignment=Qt.AlignCenter)

        # 4. 底部区域：版权信息
        footer_label = QLabel("© 2024 公司名称 版权所有")
        footer_label.setStyleSheet("color: white; font-size: 12px;")
        footer_label.setAlignment(Qt.AlignCenter)
        main_layout.addWidget(footer_label)

        # 设置主布局
        self.setLayout(main_layout)

        # 定时更新时间
        '''
        timer = QTimer(self)
        timer.timeout.connect(self.update_time)
        timer.start(1000)'''

    def update_time(self):
        """更新时间显示"""
        current_time = QDateTime.currentDateTime().toString("yyyy-MM-dd hh:mm:ss")
        self.label_time.setText(f"当前时间: {current_time}")

    def resizeEvent(self, event):
        """让背景 GIF 自适应窗口大小"""
        super().resizeEvent(event)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = LoginWindow()
    window.show()
    sys.exit(app.exec_())
